import { Action, ActionCreator } from 'redux';
import { Thread } from '../models/Thread';
import { Message } from '../models/message';
import { uuid } from '../util/uuid';

export const ADD_THREAD = '[Thread] Add';
export interface AddThreadAction extends Action {
    thread: Thread;
}
export const addThreadAction: ActionCreator<AddThreadAction> = (thread): AddThreadAction => ({
    type: ADD_THREAD,
    thread: thread
});



export const ADD_MESSAGE = '[Thread] Add Message';
export interface AddMessageAction extends Action {
    thread: Thread;
    message: Message;
}
export const addMessageAction: ActionCreator<AddMessageAction> =
    (thread: Thread, messageArgs: Message): AddMessageAction => {
        const defaults: Message = {
            id: uuid(),
            sendAt: new Date(),
            isRead: false,
            thread: thread,
            author: null,
            text: null
        };
        const message: Message = Object.assign({}, defaults, messageArgs);

        return {
            type: ADD_MESSAGE,
            thread: thread,
            message: message
        };
    };


export const SELECT_THREAD = '[Thread] Select';
export interface SelectThreadAction extends Action {
    thread: Thread;
}
export const selectThreadAction = (thread: Thread): SelectThreadAction => ({
    type: SELECT_THREAD,
    thread: thread
});
